package br.elsa.survey.services.impl;

import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import br.elsa.survey.dao.SurveyDao;
import br.elsa.survey.domain.survey.Survey;
import br.elsa.survey.services.SurveyService;
import br.elsa.survey.services.events.SurveyCreatedEvent;

/**
 * Implementation class For {@link SurveyService}.
 * 
 * <p>
 * This Service will implement all needed business logic process
 * for manipulating a {@link Survey} concept in the system.
 * </p>
 */
@Stateful
public class SurveyServiceBean implements SurveyService {

	/**
	 * DAO component responsible to implements database funcionalities
	 * for {@link Survey} entity.
	 */
	@EJB
	private SurveyDao surveyDao;
	
	/**
	 * Event fired when a {@link Survey} is created
	 */
	@Inject
	private Event<SurveyCreatedEvent> surveyCreatedEvent;
	
	public SurveyServiceBean() {
		
	}
	
	/**
	 * Constructor used by test classes to inject Mocked dependencies
	 * @param surveyDao Mocked dependency
	 * @param surveyCreatedEvent Mocked dependency
	 */
	public SurveyServiceBean(SurveyDao surveyDao, Event<SurveyCreatedEvent> surveyCreatedEvent) {
		this.surveyDao = surveyDao;
		this.surveyCreatedEvent = surveyCreatedEvent;
	}

	@Override
	public void create(Survey survey) {
		surveyDao.insert(survey);
		surveyCreatedEvent.fire(new SurveyCreatedEvent(survey));
	}

}
